package order;

public class Order implements Comparable<Order> {
    private final long id;
    private final long timeMarker;
    private final double price;
    private final int size;
    private final String clientName;

    public Order(long id, double price, int size, String clientName) {
        this.id = id;
        this.timeMarker = getTimeMarkerOfOrder();
        this.price = price;
        this.size = size;
        this.clientName = clientName;
    }

    public Order(long id, UnderdoneOrder underdoneOrder) {
        this(id, underdoneOrder.getPrice(), underdoneOrder.getSize(),
                underdoneOrder.getClientName());
    }

    private static long getTimeMarkerOfOrder() {
        return System.currentTimeMillis();
    }

    @Override
    public int compareTo(Order o) {
        final int BEFORE = -1;
        final int EQUAL = 0;
        final int AFTER = 1;

        if ( this == o ) return EQUAL;

        if (this.price < o.price) return BEFORE;
        if (this.price > o.price) return AFTER;

        if (this.timeMarker < o.timeMarker) return BEFORE;
        if (this.timeMarker > o.timeMarker) return AFTER;

        if (this.id < o.id) return BEFORE;
        if (this.id > o.id) return AFTER;

        return EQUAL;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Order order = (Order) o;
        if (id != order.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = (int) (id ^ (id >>> 32));
        result = 31 * result + (int) (timeMarker ^ (timeMarker >>> 32));
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + size;
        return result;
    }

    public double getPrice() {
        return price;
    }

    public int getSize() {
        return size;
    }

    public long getId() {
        return id;
    }

    public String getClientName() {
        return clientName;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", timeMarker=" + timeMarker +
                ", price=" + price +
                ", size=" + size +
                ", clientName='" + clientName + "\'}";
    }
}
